package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.dialog.ShopDialog;

/**
 * Created by xlm on 2017/11/8 0008
 */

public class SearchTerminalActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.begin_search)
    private TextView mTextViewSearch;

    @Inject(R.id.choose_shop)
    private TextView mTextViewShop;
    @Inject(R.id.terminal_name)
    private EditText mEditTextName;
    @Inject(R.id.terminal_id)
    private EditText mEditTextID;
    private String shopId;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_terminal_search);
    }

    @Override
    protected void initViews() {
        mViewTitle.setText("终端搜索");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mTextViewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (!"请选择门店".equals(mTextViewShop.getText().toString())) {
                    intent.putExtra("shop_id", shopId);
                }
                if (!CommonUtil.isEmpty(mEditTextID.getText().toString())) {
                    intent.putExtra("terminal_id", mEditTextID.getText().toString());
                }
                if (!CommonUtil.isEmpty(mEditTextName.getText().toString())) {
                    intent.putExtra("terminal_name", mEditTextName.getText().toString());
                }
                if ("请选择门店".equals(mTextViewShop.getText().toString())
                        && CommonUtil.isEmpty(mEditTextID.getText().toString())
                        && CommonUtil.isEmpty(mEditTextName.getText().toString())) {
                    showToast("请至少选择一种搜索条件");
                    return;
                }
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTextViewShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShopDialog();
            }
        });
    }


    private ShopDialog mShopDialog;

    /**
     * 选择门店信息
     */
    private void showShopDialog() {
        if (mShopDialog == null) {
            mShopDialog = new ShopDialog(getThis());
        }
        mShopDialog.setCancelable(true);
        mShopDialog.setShopDialogListener(new ShopDialog.ShopDialogListener() {
            @Override
            public void onItemSelected(String name, String id) {
                super.onItemSelected(name, id);
                mTextViewShop.setText(name);
                shopId = id;
            }
        });
        mShopDialog.show();
    }

}
